import Vue from 'vue'
import Vuex from 'vuex'
import user from '@/store/modules/user'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    hasLogin: false,
    access_token: "",
    userInfo: ""
  },
  mutations: {
    login(state) {
      state.hasLogin = true
    },
    logout(state) {
      state.access_token = ""
      state.userInfo = ""
      state.hasLogin = false
      //清除缓存token
      localStorage.removeItem('userInfo')
      //清除缓存userInfo
      localStorage.removeItem('access_token')
    },
    setAccessToken(state, access_token) {
      state.access_token = access_token
      //token存入缓存
      localStorage.setItem('access_token', access_token)
    },
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo
      //userInfo存入缓存
      localStorage.setItem('userInfo', JSON.stringify(userInfo))
    },
    checkLogin(state) {
      state.userInfo = JSON.parse(localStorage.getItem('userInfo')) || ''
      state.access_token = localStorage.getItem('access_token') || ''
      if (state.userInfo && state.access_token) {
        state.hasLogin = true
      } else {
        state.userInfo = state.access_token = ''
        state.hasLogin = false
      }
    }
  }
})
export default store
